
#pragma once

#include <thread>
#include <atomic>
#include "GameTime.h"
#include "../../CommonLib/src/FrameAllocator.h"

namespace FlameEngine
{
	class RenderEngine;

	class CoreEngine
	{
	public:
		CoreEngine(char* cmdLine);
		~CoreEngine();

		friend class CoreEngineHelper;

		void Init(RenderEngine* pRenderEngine);
		
		//Start and run engine 
		void StartRun();

		//
		inline RenderEngine* GetRenderEngine();

		//
		inline uint32_t GetFrameCount();

		//
		inline CoreEngineHelper* GetEngineHelper();

	protected:
		void OnSystemStart();
		void OnSystemShutdown();
		void Run();

		inline FrameAllocatorThreadSafe* GetFrameAllocator();

	private:
		std::thread m_engineThread;		
		bool m_inited;

		GameTimer m_realTimer;
		GameTimer m_logicTimer;
		GameTime m_gameTime;
		
		std::atomic<uint32_t> m_frameCount;

		double m_updateInterval;

		//reference to render engine
		RenderEngine* m_pRenderEngine;

		//
		TripleFrameAllocatorThreadSafe m_frameAllocator;

		//
		CoreEngineHelper* m_pEngineHelper;

		void StartGameLoop();
		void UpdateTime();
	};

	inline RenderEngine* CoreEngine::GetRenderEngine(){ return m_pRenderEngine; }

	inline uint32_t CoreEngine::GetFrameCount(){ return m_frameCount; }

	inline CoreEngineHelper* CoreEngine::GetEngineHelper()
	{
		return m_pEngineHelper;
	}

	inline FrameAllocatorThreadSafe* CoreEngine::GetFrameAllocator()
	{
		return m_frameAllocator.GetCurrentAlloc();
	}
}